package cn.gailvlun.gll.presentation.profile;

import android.content.Context;
import android.content.Intent;
import android.content.res.TypedArray;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.github.markzhai.recyclerview.BaseViewAdapter;

import java.util.ArrayList;
import java.util.List;

import cn.gailvlun.gll.R;
import cn.gailvlun.gll.databinding.ActivityProfileBinding;
import cn.gailvlun.gll.presentation.base.BaseActivity;
import cn.gailvlun.gll.presentation.charge.ChargeActivity;
import cn.gailvlun.gll.presentation.profile.adapter.ProfileAdapter;
import cn.gailvlun.gll.presentation.profile.bean.ProfileViewModel;
import cn.gailvlun.gll.util.ClipboardUtil;
import cn.gailvlun.gll.util.ProfileUtil;
import cn.gailvlun.gll.widget.NavigationBarView;



public class ProfileActivity extends BaseActivity {

    public static void openActivity(Context context) {
        Intent intent = new Intent(context, ProfileActivity.class);
        context.startActivity(intent);
    }

    private ActivityProfileBinding mBinding;

    private ProfileAdapter mAdapter;
    private LinearLayoutManager mLayoutManager;

    @Override
    protected void initComponent() {
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_profile);

        mAdapter = new ProfileAdapter(mContext);
        mAdapter.setPresenter(new Presenter());
        mBinding.recyclerView.setAdapter(mAdapter);

        mLayoutManager = new LinearLayoutManager(mContext);
        mBinding.recyclerView.setLayoutManager(mLayoutManager);
    }

    @Override
    protected void createHandler() {
        mBinding.nbv.setOnBackListener(new NavigationBarView.OnBackListener() {
            @Override
            public void onBack(NavigationBarView nb) {
                onBackPressed();
            }
        });
    }

    @Override
    protected void loadData(@Nullable Bundle savedInstanceState) {
        String[] titles = getResources().getStringArray(R.array.profile_title);
        TypedArray ta = getResources().obtainTypedArray(R.array.profile_icon);
        String[] visibilities = getResources().getStringArray(R.array.profile_visibility);

        List<ProfileViewModel> list = new ArrayList<>();
        for (int i = 0; i < titles.length; i++) {
            ProfileViewModel pvm =
                    new ProfileViewModel(i, titles[i],
                            ta.getResourceId(i, R.drawable.img_header_placeholder), visibilities[i]);
            if (pvm.isVisible()) {
                list.add(pvm);
            }
        }
        ta.recycle();

        mAdapter.addAll(list);
    }

    @Override
    protected void onStart() {
        super.onStart();

        mBinding.setItem(ProfileUtil.getProfile());
    }

    public class Presenter implements BaseViewAdapter.Presenter {
        public void onSelected(ProfileViewModel item) {
            switch (item.getOrder()) {
                case 0: {
                    CompleteInfoActivity.openActivity(mContext);
                    break;
                }
                case 1: {
                    ChargeActivity.Companion.openActivity(mContext);
                    break;
                }
                case 2: {
                    ComplainActivity.openActivity(mContext);
                    break;
                }
                case 3: {
                    SettingActivity.openActivity(mContext);
                    break;
                }
            }
        }

        public void copyToClipboard(String gaiId) {
            ClipboardUtil.copyToClipBoard(mContext, "id", gaiId);
        }
    }

    public static class Util {
        public static int getVisibility(ProfileViewModel item) {
            return item.isVisible() ? View.VISIBLE : View.GONE;
        }
    }
}
